package edu.bw.web.role;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.InsertRoleForm;
import edu.bw.service.RoleService;
import edu.bw.service.impl.RoleServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/role/insert")
public class InsertRoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{"id":0,"roleName":"测试001","permissions":[1,2,3],"desc":"测试001","changed":true}
        //1.解析前端请求的数据
        String reqJSON = request.getReader().readLine();
        InsertRoleForm insertRoleForm = JSONUtil.toBean(reqJSON, InsertRoleForm.class);

        //2.调用service层处理数据
        Integer rows = roleService.insert(insertRoleForm);

        //3.向前端响应数据
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
